package com.winit.openapi.wh.allocation.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.allocation.command.order.AllocationFeeEstimateCommand;
import com.winit.oms.spi.allocation.service.fee.AllocationOrderFeeCalculateService;
import com.winit.oms.spi.vo.v2.response.FeeDetailResult;
import com.winit.oms.spi.vo.v2.response.FeeEstimateResult;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

@Controller
@RequestMapping(value = "/wh/allocation")
public class AllocationFeeEstimateController extends BaseController {

    @Resource
    private AllocationOrderFeeCalculateService allocationOrderFeeCalculateService;

    @RequestMapping(value = "/estimated")
    public String estimated(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject jsonObj = (JSONObject) obj;
            ValidateUtil.validNotNull(jsonObj, "orderNo");
            AllocationFeeEstimateCommand command = new AllocationFeeEstimateCommand();
            command.setOrderNo(jsonObj.getString("orderNo"));
            FeeEstimateResult fs = allocationOrderFeeCalculateService.estimated(command);
            if (fs != null) {
                Map<String, Object> datalist = buildOrderList(fs);
                responseMsg.setData(datalist);
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }

    private Map<String, Object> buildOrderList(FeeEstimateResult result) {
        Map<String, Object> mapt = new HashMap<String, Object>();
        mapt.put("chargeWeight", result.getWeight() == null ? "" : result.getWeight().toPlainString());
        mapt.put("chargeVolume", result.getVolume() == null ? "" : result.getVolume().toPlainString());
        mapt.put("esFrozenAmount", result.getTotalAmount() == null ? "" : result.getTotalAmount().toPlainString());
        mapt.put("accountBalance", result.getAccountBalance());
        mapt.put("currency", result.getCurrency());
        List<FeeDetailResult> list = result.getFeeDetail();
        List<Map<String, Object>> ms = new ArrayList<Map<String, Object>>();
        for (FeeDetailResult feeDetailResult : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("chargeType", feeDetailResult.getChargeName() == null ? "" : feeDetailResult.getChargeName());
            map.put("offerAmount", feeDetailResult.getAmount() == null ? "" : feeDetailResult.getAmount()
                .toPlainString());
            map.put("currency", feeDetailResult.getCurrency() == null ? "" : feeDetailResult.getCurrency());
            map.put("chargeAmount", feeDetailResult.getAmount() == null ? "" : feeDetailResult.getAmount()
                .toPlainString());
            map.put("standardCurrency", feeDetailResult.getCurrency() == null ? "" : feeDetailResult.getCurrency());
            ms.add(map);
        }
        mapt.put("estimateChargeList", ms);
        return mapt;
    }
}
